package com.howie.gulimall.search.controller;

import com.howie.common.exception.BizCodeEnum;
import com.howie.common.to.es.SkuEsModel;
import com.howie.common.utils.R;
import com.howie.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @description: 控制层
 * @author: laizhenghua
 * @date: 2021/1/23 9:53
 */
@RestController
@RequestMapping("/search/save")
@Slf4j
public class ElasticSearchSaveController {
    @Autowired
    private ProductSaveService productSaveService;
    /**
     * 上架商品(把商品信息保存到es内存里)
     * 请求路径：/search/save/product
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> models){
        boolean hasFailure = false; // 是否有失败的(未保存到es)
        try{
            hasFailure = productSaveService.productStatusUp(models);
        }catch(Exception e){
            log.error("ElasticSearchSaveController商品上架错误！{}:{}",e.getClass(),e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
        if(!hasFailure){
            return R.ok();
        }else{
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }
}
